package com.hubujun.driverlicense;

import com.hubujun.driverlicense.core.LZHandlerThread;
import com.hubujun.driverlicense.db.AccountStorage;
import com.hubujun.driverlicense.util.FileUtils;
import com.hubujun.driverlicense.util.Util;

import java.io.File;

/**
 * Created by Administrator on 2016/5/28 0028.
 */
public class DLCore {
    private static DLCore mCore;
    private static String filePath;
    private AccountStorage mStorage;
    private static String cachePath;
    private LZHandlerThread handlerThread = new LZHandlerThread();
    public static DLCore getInstance() {
        return mCore;
    }

    public static void init() {
        mCore = new DLCore();
    }

    private DLCore() {
        initPath();
        // 初始化账户相关存储系统
        mStorage = new AccountStorage(filePath);
        Thread storageThread = new Thread() {
            @Override
            public void run() {
                mStorage.init();
            }
        };
        storageThread.start();
    }

    public static String initPath() {
        cachePath = ApplicationContext.getContext().getCacheDir().getAbsolutePath() + "/";
        filePath = ApplicationContext.getContext().getFilesDir().getAbsolutePath() + "/";

        if (Util.canWriteExternal()) {
            cachePath = DLExternalPath.DL_CACHE;
            filePath = DLExternalPath.DL_FILE;
        }

        File f = new File(cachePath);
        if (!f.exists())
            f.mkdirs();
        f = new File(filePath);
        if (!f.exists())
            f.mkdirs();
        f = new File(getImagePath());
        if (!f.exists())
            f.mkdirs();
        f = new File(getMediaPath());
        if (!f.exists())
            f.mkdirs();
        f = new File(getTempPath());
        if (!f.exists())
            f.mkdirs();
        f = new File(getHistoryPath());
        if (!f.exists())
            f.mkdirs();
        return filePath;
    }


    public static String getCachePath() {
        return cachePath;
    }

    public static String getImagePath() {
        FileUtils.createDir(cachePath + "Image/");
        return cachePath + "Image/";
    }

    public static String getMediaPath() {
        FileUtils.createDir(cachePath + "media/");
        return cachePath + "media/";
    }

    public static String getTempPath() {
        FileUtils.createDir(cachePath + "tmp/");
        return cachePath + "tmp/";
    }

    public static String getHistoryPath() {
        FileUtils.createDir(cachePath + "history/");
        return cachePath + "history/";
    }

    public static String getSourcePath() {
        FileUtils.createDir(filePath + "source/");
        return filePath + "source/";
    }

    public AccountStorage getAccountStorage() {
        return mStorage;
    }


    public static LZHandlerThread getHandleThread() {
        return getInstance().handlerThread;
    }
}
